package com.lx.backstagemanagement.web;

import com.lx.backstagemanagement.RabbitMqProduct.SimpleProduct;
import com.lx.backstagemanagement.common.ResultCode;
import com.lx.backstagemanagement.entity.Category;
import com.lx.backstagemanagement.service.ICategoryService;
import com.lx.backstagemanagement.util.UUIDUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lee
 * @date 2018年6月26日 11:33:52
 */
@Api
@RestController
@RequestMapping(value = "/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryServiceImpl;
    @Autowired
    private SimpleProduct simpleProduct;

    @GetMapping
    public ResultCode list() {
        List<Category> categorys = categoryServiceImpl.categorys();
        return ResultCode.getSuccessReturn(categorys);
    }

    @GetMapping(value = "/{cid}")
    public ResultCode getCategoryBycid(@PathVariable(value = "cid") String cid) {
        Category byCid = categoryServiceImpl.getByCid(cid);
        return ResultCode.getSuccessReturn(byCid);
    }

    @PostMapping(value = "/{cid}")
    public ResultCode edit(Category category, @PathVariable(value = "cid") String cid) {
        category.setCid(cid);
        categoryServiceImpl.edit(category);
//        simpleProduct.sender("category");
        return ResultCode.SUCCESS;
    }

    @DeleteMapping(value = "/{cid}")
    public ResultCode delete(@PathVariable(value = "cid") String cid) {
        categoryServiceImpl.deleteCategory(cid);
//        simpleProduct.sender("category");
        return ResultCode.SUCCESS;
    }

    @PostMapping
    public ResultCode add(Category category) {
        category.setCid(UUIDUtils.getUUID());
        categoryServiceImpl.add(category);
//        simpleProduct.sender("category");
        return ResultCode.SUCCESS;
    }


}
